﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using roguelike;


namespace UI
{

    public class FinishedValueConverter : IValueConverter
    { 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool toReturn = (bool)value;
            if (toReturn)
            return System.Windows.Media.Brushes.Green;
            return System.Windows.Media.Brushes.Red;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return false;
        }
    }
    public class MainViewModel : ViewModelBase
    {
        private int[,] world;
        int playerX;
        int playerY;
        public string PlayerName {get { return "Abram"; }}
        public Point PlayerPosition;
        Bitmap woodFloor;
        Bitmap stoneWall;
      
        Bitmap playerImg;
        System.Windows.Controls.Image tempImg = new System.Windows.Controls.Image();

        public MainViewModel()
        {
           
            LoadingRes();
            //CreateMatrix(250,250);
        }

        public int[,] CreateMatrix(int h,int w)
        {
            const int blackEndOfWorld = 400; //это кромка черного поля вокруг данжа;
            PlayerPosition.X=20;
            PlayerPosition.Y=20;
            var map = new Map(h,w);
            world = map.Matrix;


            var temp = new int[world.GetLength(0) + blackEndOfWorld, world.GetLength(1) + blackEndOfWorld];

            for (int i = 0; i < world.GetLength(0); i++)
            {
                for (int j = 0; j < world.GetLength(1); j++)
                {
                    temp[i + blackEndOfWorld / 2, j + blackEndOfWorld/2] = world[i, j];
                }
            }
            world = temp;
            var rand = new Random();
            //while (world[PlayerPosition.X+playerX,PlayerPosition.Y+playerY]!=2)
            //{
            //    PlayerPosition.X = rand.Next(blackEndOfWorld/2, world.GetLength(0) - blackEndOfWorld/2);
            //    PlayerPosition.Y = rand.Next(blackEndOfWorld/2, world.GetLength(0) - blackEndOfWorld/2);
            //} 
            PlayerPosition.X = world.GetLength(0)/4;
            PlayerPosition.Y = world.GetLength(0)/3;
            return world;
        }

        public void LoadingRes()
        {
             woodFloor=new Bitmap(@"Res\MapRes\WoodFloor.png");
             stoneWall = new Bitmap(@"Res\MapRes\wall.png");
             playerImg=new Bitmap(@"Res\MapRes\Person.png");
        }

        public System.Windows.Controls.Image  GeneratePicture(int width,int height)
        {
            const short tilesize = 5;
            playerX =(width/tilesize)/2;
            playerY = (height / tilesize)/2;
            var bm = new Bitmap(width+1, height+1);
            Graphics g = Graphics.FromImage(bm);

            for (int i = 0; i < (width / tilesize) + 1; i++)
            {
                for (int j = 0; j < (height / tilesize) + 1; j++)
                {
                    switch (world[i+PlayerPosition.X,j+PlayerPosition.Y])
                    {
                        case 1:
                        case 3:
                            g.DrawImage(stoneWall, i * tilesize, j * tilesize, tilesize, tilesize);
                            break;
                        case 0:
                            break;
                        case 2:
                        case 4:
                            g.DrawImage(woodFloor, i * tilesize, j * tilesize, tilesize, tilesize);
                        break;
                    }
                }
            }
            g.DrawImage(playerImg,playerX * tilesize, playerY * tilesize, tilesize, tilesize);

            tempImg = new System.Windows.Controls.Image();
            using (var memory = new MemoryStream())
            {
                bm.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                tempImg.Source = bitmapImage;
            }
            g.Dispose();
            bm.Dispose();
            return tempImg;
        }

        public void PlayerMove(Key key)
        {
            switch (key)
            {
                case Key.Right:
                    if (world[PlayerPosition.X + playerX+1, PlayerPosition.Y+playerY] != 1 && world[PlayerPosition.X + playerX+1, PlayerPosition.Y+playerY] != 3)
                    {
                        PlayerPosition.X++;
                    }
                    break;
                case Key.Left:
                    if (world[PlayerPosition.X+playerX-1, PlayerPosition.Y+playerY] != 1 && world[PlayerPosition.X+playerX-1, PlayerPosition.Y+playerY] != 3)
                    {
                        PlayerPosition.X--;
                    }
                    break;
                case Key.Up:
                    if (world[PlayerPosition.X + playerX, PlayerPosition.Y + playerY - 1] != 1 && world[PlayerPosition.X + playerX, PlayerPosition.Y + playerY - 1] != 3)
                    {
                        PlayerPosition.Y--;
                    }
                    break;
                case  Key.Down:
                    if (world[PlayerPosition.X + playerX, PlayerPosition.Y + playerY + 1] != 1 && world[PlayerPosition.X + playerX, PlayerPosition.Y + playerY + 1] != 3)
                    {
                        PlayerPosition.Y++;
                    }
                    break;
            }           
        }
        protected override void UpdateProperties()
        {   
            OnPropertyChanged(() =>PlayerName);
        }
    }
}
